<?php

namespace app\model;

use think\Model;

class RoleEquip extends Model
{
    protected $name = 'playerzhuangbei';

    protected $pk = 'zbnowid';

    protected $schema = [
        'zbnowid' => 'int',

        'uid' => 'int',
        'sid' => 'string',

        'zbid' => 'int',
        'zblv' => 'int',
        'zbtool' => 'int',
        'zbname' => 'string',
        'zbinfo' => 'string',

        'zbhp' => 'int',

        'zbgj' => 'int',
        'zbfy' => 'int',
        'zbbj' => 'int',
        'zbxx' => 'int',
        'zbsd' => 'int',

        'qianghua' => 'int',
        'status'   => 'int',    //状态，1正常装备栏中，2穿戴中，3寄售中，4仓库中
    ];

    /**
     * 获取列表
     */
    public function loadList($where = [], $pageAry = [], $order = 'zbnowid desc')
    {
        $data = $this->where($where)->order($order)->paginate($pageAry);
        return $data;
    }

    /**
     * 获取列表
     */
    public function getList($where = array(), $order = 'zbnowid desc')
    {
        $data = $this->where($where)->order($order)->select();
        return $data;
    }

    /**
     * 获取信息
     * @param int $ID
     * @return array 信息
     */
    public function getInfo($id)
    {
        $map = array();
        $map['zbnowid'] = $id;
        return $this->getWhereInfo($map);
    }

    /**
     * 获取信息
     * @param array $where 条件
     * @return array 信息
     */
    public function getWhereInfo($where)
    {
        $info = $this->where($where)->find();
        return $info;
    }

    /**
     * 获取字段值
     * @param array $where 条件
     * @param array $field 字段
     * @return field 信息
     */
    public function getWhereValue($where, $field)
    {
        $data = $this->where($where)->value($field);
        return $data;
    }

    /**
     * 获取列的值
     * @param array $where 条件
     * @param array $field 字段
     * @return array 信息
     */
    public function getWhereColumn($where, $field)
    {
        $data = $this->where($where)->column($field);
        return $data;
    }

    /**
     * 新增
     */
    public function addall($request)
    {
        return $this->save($_POST);
    }

    /**
     * 更新
     */
    public function editAll($request)
    {
        return $this->saved($_POST);
    }

    //数据新增
    public function add($data)
    {
        //启动事务
        $this->startTrans();
        try {
            $insertid = self::insertGetId($data);
            //提交事务
            $this->commit();
            return $insertid;
        } catch (\Exception $e) {
            //回滚事务
            $this->rollback();
        }
    }

    //数据更新
    public function saved($data)
    {
        $this->update($data);
        return true;
    }

    public function findValue($where, $field)
    {
        return $this->where($where)->value($field);
    }

    /**
     * 删除信息
     * @param int $id ID
     * @return bool 删除状态
     */
    public function del($id)
    {
        $map = array();
        $map['zbnowid'] = $id;
        return $this->where($map)->delete();
    }

    public function addzb($uid, $id)
    {
        $data = model('Equip')->getInfo($id)->toArray();
        $zbtool = model('Equip')->zbtool();
        $zbtool = array_flip($zbtool);
        
        $add = [
            'uid' => $uid,
            
            'zbid' => $data['zbid'],
            'zblv' => $data['zblv'],
            'zbtool' => $zbtool[$data['zbtool']],
            'zbname' => $data['zbname'],
            'zbinfo' => $data['zbinfo'],
            'zbhp' => $data['zbhp'],
            'zbgj' => $data['zbgj'],
            'zbfy' => $data['zbfy'],
            'zbbj' => $data['zbbj'],
            'zbxx' => $data['zbxx'],
            'zbsd' => $data['zbsd'],
        ];

        return $this->add($add);
    }

}
